using BXS.API.Models;
using Microsoft.AspNetCore.Mvc;

namespace BXS.API.Controllers
{
    /// <summary>
    /// 分类管理控制器
    /// </summary>
    public class CategoriesController : BaseController
    {
        // 模拟数据
        private static List<Category> _categories = new List<Category>
        {
            new Category { Id = 1, Name = "寿衣", Type = "product", Icon = "👕", Sort = 1, Status = "active", CreateTime = "2024-01-01", ProductCount = 15 },
            new Category { Id = 2, Name = "骨灰容器", Type = "product", Icon = "🏺", Sort = 2, Status = "active", CreateTime = "2024-01-02", ProductCount = 8 },
            new Category { Id = 3, Name = "鲜花花艺", Type = "product", Icon = "🌸", Sort = 3, Status = "active", CreateTime = "2024-01-03", ProductCount = 25 },
            new Category { Id = 4, Name = "纸扎祭品", Type = "product", Icon = "📄", Sort = 4, Status = "active", CreateTime = "2024-01-04", ProductCount = 12 },
            new Category { Id = 5, Name = "纪念品", Type = "product", Icon = "🖼️", Sort = 5, Status = "inactive", CreateTime = "2024-01-05", ProductCount = 6 },
            new Category { Id = 6, Name = "法事服务", Type = "service", Icon = "🙏", Sort = 6, Status = "active", CreateTime = "2024-01-06", ProductCount = 3 },
            new Category { Id = 7, Name = "祭扫服务", Type = "service", Icon = "🕯️", Sort = 7, Status = "active", CreateTime = "2024-01-07", ProductCount = 5 },
            new Category { Id = 8, Name = "遗体处理", Type = "service", Icon = "⚰️", Sort = 8, Status = "active", CreateTime = "2024-01-08", ProductCount = 2 },
        };

        /// <summary>
        /// 获取分类列表
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="type">类型筛选</param>
        /// <returns>分类列表</returns>
        [HttpGet]
        public ActionResult<PagedApiResponse<Category>> GetCategories([FromQuery] int page = 1, [FromQuery] int pageSize = 10, [FromQuery] string? type = null)
        {
            var query = _categories.AsQueryable();
            
            if (!string.IsNullOrEmpty(type) && type != "all")
            {
                query = query.Where(c => c.Type == type);
            }

            var total = query.Count();
            var skip = (page - 1) * pageSize;
            var categories = query.OrderBy(c => c.Sort).Skip(skip).Take(pageSize).ToList();
            
            return PagedSuccess(categories, total, page, pageSize);
        }

        /// <summary>
        /// 根据ID获取分类
        /// </summary>
        /// <param name="id">分类ID</param>
        /// <returns>分类信息</returns>
        [HttpGet("{id}")]
        public ActionResult<ApiResponse<Category>> GetCategory(int id)
        {
            var category = _categories.FirstOrDefault(c => c.Id == id);
            if (category == null)
            {
                return Error<Category>("分类不存在", 404);
            }
            return Success(category);
        }

        /// <summary>
        /// 创建分类
        /// </summary>
        /// <param name="request">创建分类请求</param>
        /// <returns>创建结果</returns>
        [HttpPost]
        public ActionResult<ApiResponse<Category>> CreateCategory([FromBody] CreateCategoryRequest request)
        {
            if (string.IsNullOrEmpty(request.Name) || string.IsNullOrEmpty(request.Type))
            {
                return Error<Category>("请填写完整信息");
            }

            var newCategory = new Category
            {
                Id = _categories.Count > 0 ? _categories.Max(c => c.Id) + 1 : 1,
                Name = request.Name,
                Type = request.Type,
                Icon = request.Icon,
                Sort = request.Sort,
                Status = request.Status,
                CreateTime = DateTime.Now.ToString("yyyy-MM-dd"),
                ProductCount = 0
            };

            _categories.Add(newCategory);
            return Success(newCategory, "分类创建成功");
        }

        /// <summary>
        /// 更新分类
        /// </summary>
        /// <param name="id">分类ID</param>
        /// <param name="request">更新分类请求</param>
        /// <returns>更新结果</returns>
        [HttpPut("{id}")]
        public ActionResult<ApiResponse<Category>> UpdateCategory(int id, [FromBody] UpdateCategoryRequest request)
        {
            var category = _categories.FirstOrDefault(c => c.Id == id);
            if (category == null)
            {
                return Error<Category>("分类不存在", 404);
            }

            if (string.IsNullOrEmpty(request.Name) || string.IsNullOrEmpty(request.Type))
            {
                return Error<Category>("请填写完整信息");
            }

            category.Name = request.Name;
            category.Type = request.Type;
            category.Icon = request.Icon;
            category.Sort = request.Sort;
            category.Status = request.Status;

            return Success(category, "分类更新成功");
        }

        /// <summary>
        /// 删除分类
        /// </summary>
        /// <param name="id">分类ID</param>
        /// <returns>删除结果</returns>
        [HttpDelete("{id}")]
        public ActionResult<ApiResponse<bool>> DeleteCategory(int id)
        {
            var category = _categories.FirstOrDefault(c => c.Id == id);
            if (category == null)
            {
                return Error<bool>("分类不存在", 404);
            }

            if (category.ProductCount > 0)
            {
                return Error<bool>("该分类下还有商品，无法删除");
            }

            _categories.Remove(category);
            return Success(true, "分类删除成功");
        }

        /// <summary>
        /// 获取分类类型列表
        /// </summary>
        /// <returns>类型列表</returns>
        [HttpGet("types")]
        public ActionResult<ApiResponse<List<string>>> GetTypes()
        {
            var types = _categories.Select(c => c.Type).Distinct().ToList();
            return Success(types);
        }
    }
} 